package com.hanhai.zrb.mobile.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hanhai.common.annotation.LoginNeedless;
import com.hanhai.zrb.api.biz.service.CityInfoService;
import com.hanhai.zrb.mobile.result.BaseApiResult;
import com.hanhai.zrb.model.project.CityInfo;


@LoginNeedless
@RestController
@RequestMapping("/areaApi")
public class AreaApiController {
	
	@Resource
	private CityInfoService cityInfoService;

	@RequestMapping("findAllProvince")
	public BaseApiResult findAllProvince(HttpServletResponse response) throws IOException {
		List<CityInfo> list =cityInfoService.findAllProvince();//省
		BaseApiResult result = new BaseApiResult();
		result.setData(list);
        return result;
	}
	
	@RequestMapping("findCityByProvince")
	public BaseApiResult findCityByProvince(String code,HttpServletResponse response) throws IOException {
		List<CityInfo> list = new ArrayList<CityInfo>();
		CityInfo cityInfo = new CityInfo();
		if( "110000".equals(code)){//北京
//			cityInfo.setPostcode(code);
//			cityInfo.setCityName("北京市");
//			list.add(cityInfo);
		 	list = cityInfoService.findCountryProvince(code.substring(0, 2),"北京市");
		}else if( "120000".equals(code)){//天津市
//			cityInfo.setPostcode(code);
//			cityInfo.setCityName("天津市");
//			list.add(cityInfo);
			list = cityInfoService.findCountryProvince(code.substring(0, 2),"天津市");
		}else if( "500000".equals(code)){//重庆市
//			cityInfo.setPostcode(code);
//			cityInfo.setCityName("重庆市");
//			list.add(cityInfo);
			list = cityInfoService.findCountryProvince(code.substring(0, 2),"重庆市");
		}else if( "310000".equals(code)){//上海
//			cityInfo.setPostcode(code);
//			cityInfo.setCityName("上海市");
//			list.add(cityInfo);
			list = cityInfoService.findCountryProvince(code.substring(0, 2),"上海市");
		}else if( "710000".equals(code)){//台湾省
			cityInfo.setPostcode(code);
			cityInfo.setCityName("台湾省");
			list.add(cityInfo);
		}else if( "810000".equals(code)){//香港特别行政区
			cityInfo.setPostcode(code);
			cityInfo.setCityName("香港特别行政区");
			list.add(cityInfo);
		}else if( "820000".equals(code)){//澳门特别行政区
			cityInfo.setPostcode(code);
			cityInfo.setCityName("澳门特别行政区");
			list.add(cityInfo);
		}else{
			list = cityInfoService.findCityProvince(code.substring(0, 2));
		}
		BaseApiResult result = new BaseApiResult();
		result.setData(list);
        return result;
	}
	
	@RequestMapping("findDistrictByCity")
	public BaseApiResult findDistrictByCity(String code, HttpServletResponse response) throws IOException {
		//List<District> list =districtService.findDistrictByCity(code);//区县
		List<CityInfo> list = new ArrayList<CityInfo>();
		CityInfo cityInfo = new CityInfo();
		 if( "810000".equals(code)){//香港特别行政区
			cityInfo.setPostcode(code);
			cityInfo.setCityName("香港特别行政区");
			list.add(cityInfo);
		 }else if( "820000".equals(code)){//澳门特别行政区
			cityInfo.setPostcode(code);
			cityInfo.setCityName("澳门特别行政区");
			list.add(cityInfo);
		 }else if( "710000".equals(code)){//台湾省
			cityInfo.setPostcode(code);
			cityInfo.setCityName("台湾省");
			list.add(cityInfo);
		 }
		 else{
			CityInfo cinfo = cityInfoService.selectByPostcode(code);
		 	list = cityInfoService.findCountryProvince(code.substring(0, 2),cinfo.getCityName());
		 }
		BaseApiResult result = new BaseApiResult();
		result.setData(list);
        return result;
	}
	
	
	

}
